package xjs.base;
/**
 * Token Class represents the wrapping object for DOM Elements for cross-browser
 * support
 */
(function() {
	
	var DOCUMENT = document;
	/**
	 * 
	 */
	xJS.Token = function(domElement) {
		var elt = typeof domElement == 'string' ? 
				DOCUMENT.getElementById(domElement) : domElement, id;

		if (!domElement || !DR.isDOMElement(elt))
			return null;

		id = elt.id;

		// if the element with specified id is already buffered
		// don't create it again,
		// return the buffered one.
		if (id && xJS.tokenBuffer[id]) {
			return xJS.tokenBuffer[id];
		}

		this.elt = elt;
		this.id = id || xJS.generateId();
		xJS.tokenBuffer[id] = this;
	};

	var DR = xJS.DOMReader,
	    DM = xJS.DOMManipulator,
	    TOKEN = xJS.Token;

	/**
	 * 
	 */
	TOKEN.prototype = {

		/**
		 * 
		 * @param class
		 * @return
		 */
		addClass : function(cn) {
			return cn && (this.elt.className += ' ' + cn);
		},
		
		/**
		 * 
		 * @param cn
		 * @return
		 */
		removeClass : function(cn) {
			return this.hasClass(cn) ? this.elt.className = (' ' + this.elt.className).replace(' ' + cn,'') : this.elt.className;
		},
		
		/**
		 * 
		 * @param cn
		 * @param rcn
		 * @return
		 */
		replaceClass : function(cn, rcn) {
			return this.hasClass(cn) ? this.elt.className = (' ' + this.elt.className).replace(' ' + cn,' ' + rcn) : this.elt.className;
		},

		/**
		 * 
		 * @param className
		 * @return
		 */
		hasClass : function(cn) {
			return cn && (' ' + this.elt.className).indexOf(' ' + cn) != -1;
		},

		/**
		 * 
		 * @param style
		 * @return
		 */
		getStyle : function(style) {
			return this.elt.style[style];
		},
		
		/**
		 * 
		 * @param style
		 * @param value
		 * @return
		 */
		setStyle : function(style, value) {
			if (style)
			{
				this.elt.style[style] = value;
			}
 		},
 		
 		/**
 		 * 
 		 * @param opacity
 		 * @return
 		 */
 		setOpacity : function(opacity) {
 			if (xJS.isIE)
 			{
 				this.setStyle("zoom", 1);
 				this.setStyle("filter","alpha(opacity="+ (opacity*100) +")");
 			}
 			else
 			{
 				this.setStyle("opacity", opacity);
 			}
 		},
 		
 		/**
 		 * 
 		 * @return
 		 */
 		getOpacity : function() {
 			if (!xJS.isIE)
 				return this.getStyle("opacity");
 			
 			var filter = this.getStyle("filter");
 			return parseFloat(filter.substring(filter.search(/[0-9]{1}/g)))/100.0;
 			
 		}
	};
	
	/**
	 * 
	 * @param elt
	 * @return
	 */
	TOKEN.getToken = function(elt) {
		
		if (elt == DOCUMENT)
		{
			if (!documentToken)
			{
 				var F = function(){};
 				F.prototype = TOKEN.prototype;
 	            documentToken = new F();
 	            documentToken.elt = DOCUMENT;
			}
            return documenToken;
		}
		else if (elt instanceof TOKEN) 
		{
			elt.elt = DOCUMENT.getElementById(elt.id) || elt.elt;
			
			return elt;
		} 
		else if (typeof elt == 'string' || xJS.isDOMElement(elt))
		{
			return new xJS.Token(elt);
		}
			
		
		return null;
	};
	

})();

xJS.token = xJS.Token.getToken;